//导入axios
import axios from 'axios'
import { Toast } from 'vant';
import $router from '../router/index'
//实例化axios对象
var service = axios.create({
    timeout: 30 * 1000,//设置超时时间
    // baseURL: '/api'//请求地址的基准路径
    baseURL: process.env.VUE_APP_BASE_API,
})

//设置请求拦截
service.interceptors.request.use(
    (config) => {
        config.headers['Authorization'] = 'Bearer' + ' ' + localStorage.getItem('token');
        return config;
    }, (error) => {
        return Promise.reject(error);
    })
//设置响应拦截
service.interceptors.request.use(
    (res) => {
        return res;
    }, (error) => {
        if (error.response.status == 401) {
            Toast('身份认证过期，请重新登录！');
            //设置请求过期处理
            localStorage.removeItem('token')
            $router.push('/login')
        } else if (error.response.status == 404) {
            Toast('访问路径有误！');
        } else if (error.response.status == 500) {
            Toast('服务器问题');
        }
    }
)
export default service;